<html>
<head>
<script type="text/javascript">
	function Insert() {
		document.getElementById("title").required = true;
		document.getElementById("quarter").required = true;
		document.getElementById("year").required = true;
		document.getElementById("enrollmentlimit").required = true;
		document.getElementById("action").value = "insert";
	}
	function Delete(row) {
		document.getElementById("action").value = "delete";
		document.getElementById("row").value = row;

	}
</script>
</head>
<body>
	<%@ page language="java" import="java.sql.*"%>
	<%
		try {
			Class.forName("org.postgresql.Driver");
			Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/cse132b","cse132blogin", "1234567890");
			String action = request.getParameter("action");
			// Check if an insertion is requested
			if (action != null && action.equals("insert")) {
				PreparedStatement pstmt;
				pstmt = conn
							.prepareStatement("INSERT INTO classes (course_id,quarter,year,enrollment_limit,title) VALUES (?,?,?,?,?)");
				pstmt.setInt(1, Integer.parseInt(request.getParameter("courseid")));
				pstmt.setString(2,request.getParameter("quarter"));
				pstmt.setInt(3,Integer.parseInt(request.getParameter("year")));
				pstmt.setInt(4,Integer.parseInt(request.getParameter("enrollmentlimit")));
				pstmt.setString(5, request.getParameter("title"));
				int rowCount = pstmt.executeUpdate();
				out.println("Successfully made new course.<br/>");
			} else if (action != null && action.equals("delete")) {
				PreparedStatement pstmt;
				pstmt = conn.prepareStatement("DELETE FROM classes WHERE class_id = ?");
				pstmt.setInt(1, Integer.parseInt(request.getParameter("row")));
				pstmt.executeUpdate();
			}

			// Create the statement
			Statement courses = conn.createStatement();

			// Use the created statement to SELECT
			// the student attributes FROM the Student table.
			ResultSet rsCourses = courses
					.executeQuery("SELECT * FROM courses");
			
			Statement classes = conn.createStatement();
			ResultSet rsExistingClasses = classes.executeQuery("SELECT * FROM classes LEFT JOIN courses ON (classes.course_id = courses.course_id)");
	%>
	<table border="1">
		<tr>
			<td valign="top" width="40%">
				<%-- -------- Include menu HTML code -------- --%> <jsp:include
					page="/menu.html" />
			</td>
			<td>	
	<table border="0">
		<tr>
			<td valign="top">Classes Entry Page</td>
		</tr>
		<tr>
			<form action="classes.jsp" method="post">
			<input type="hidden" id="action" name="action"/>
			<input type="hidden" id="row" name="row"/>
			<table border="1">
				<tr>
					<th>Course*</th>
					<th>Title*</th>
					<th>Quarter*</th>
					<th>Year*</th>
					<th>Enrollment Limit*</th> 
				</tr>
				<tr>
						<td>
							<select id="courseid" name="courseid">
								<%
								
								while (rsCourses.next()) {
								%>
								<option value="<%= rsCourses.getInt("course_id") %>"><%= rsCourses.getString("department_abbreviation") %> <%= rsCourses.getString("course_number") %></option>
								<%
								}
								%>
							</select>
						</td>
						<td>
							<input type="text" id="title" name="title"></input>
						</td>
						<td>
							<select id="quarter" name="quarter">
								<option value="1">Fall</option>
								<option value="2">Winter</option>
								<option value="3">Spring</option>
								<option value="4">Summer Session 1</option>
								<option value="5">Summer Session 2</option>
							</select>
						</td>
						<td>
							<select id="year" name="year">
								<% for(int i=2014;i>=1990;i--) { %>
									<option value="<%=i %>"><%=i %></option>
								<% } %>
							</select>
						</td>
						<td><input type="text" id="enrollmentlimit" name="enrollmentlimit"/></td>
						<td><input type="submit" onclick="Insert()"></input></td>
				</tr>
			</table><br/>
			Existing Classes<br/> 
			<table border="1">
				<tr>
					<th>Course</th>
					<th>Title</th>
					<th>Quarter</th>
					<th>Year</th>
					<th>Enrollment Limit</th> 
				</tr>
				<%
				while(rsExistingClasses.next()) {
				%>
				<tr>
					<td><%=rsExistingClasses.getString("department_abbreviation") %> <%=rsExistingClasses.getString("course_number") %></td>
					<td><%=rsExistingClasses.getString("title") %></td>
					<td><%=rsExistingClasses.getString("quarter") %></td>
					<td><%=rsExistingClasses.getInt("year") %></td>
					<td><%=rsExistingClasses.getInt("enrollment_limit") %></td>
					<td><a href="/mytritonlink/new/sections.jsp?classid=<%=rsExistingClasses.getInt("class_id") %>">Edit Sections</a><input type="submit" value="Delete" onclick="Delete(<%=rsExistingClasses.getInt("class_id") %>)"/></td>					
				</tr>
				<%
				}
				%>
			</table>
		</tr>
	</table>
	</form>
	<%
		} catch (SQLException ex) {
			out.print("SQLException: " + ex.getMessage() + "<br/>");
			out.print("SQLState: " + ex.getSQLState() + "<br/>");
			out.print("VendorError: " + ex.getErrorCode() + "<br/>");
		}
	%>
</td>
</tr>
</table>
</body>
</html>